﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YiLe.UI;

namespace YiLe.DataForms
{
    public partial class CustomerDataForm : DataForm
    {
        protected override void StartEdition()
        {
            this.CustomerControl.UseTabBackground = true;
            this.CustomerControl.DataSource = this.DataSet;
            if (this.EditMode == DataFormEditMode.AddNew)
            {
                this.Text = "Customers - New Customer";
                this.CustomerControl.IsAddNew = true;
            }
            else
            {
                this.Text = "Customers - " + ((DataRowView)CurrencyManager.Current)["CompanyName"];
                this.CustomerControl.IsAddNew = false;
            }
            this.CustomerControl.Select();
        }


        private bool mOrdersFilled;
        private bool mProductsFilled;
        private void FillOrders()
        {
            if (!mOrdersFilled)
            {
                if (!this.DataRowView.IsNew)
                {
                    //TODO:this.DataSet.OrdersExtended
                    //this.ordersTableAdapter.Fill((String)this.DataRowView["CustomerID"], this.DataSet.OrdersExtended);
                }
                mOrdersFilled = true;
            }
        }
        private void FillProducts()
        {
            if (!mProductsFilled)
            {
                if (!this.DataRowView.IsNew)
                {
                    this.jsgProducts.RootTable.Columns["ProductID"].HasValueList = true;
                    /*this.jsgProducts.RootTable.Columns["ProductID"].ValueList.PopulateValueList(YiLeSystemApp.YiLeSystemApplication.GetProductList().DefaultView, "ProductID", "ProductName");
                    this.orderDetailsAdapter.FillByCustomer((string)this.DataRowView["CustomerID"], this.DataSet.Xs_OrderDetail);*/
                }
                this.jsgProducts.CollapseGroups();
                this.jsgProducts.Row = 0;
                mProductsFilled = true;
            }
        }



        private void UiTab1_SelectedTabChanged(object sender, Janus.Windows.UI.Tab.TabEventArgs e)
        {
            if (e.Page == this.tabOrders)
            {
                this.FillOrders();
                //TODO:DataSet.OrdersExtended
                //this.lblNoOrders.Visible = (this.DataSet.OrdersExtended.Count == 0);
            }
            else if (e.Page == this.tabProducts)
            {
                this.FillProducts();
            }
        }

        private void jsgProducts_LoadingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
        {
            if (e.Row.RowType == Janus.Windows.GridEX.RowType.Record)
            {
                //calculate the amount
                decimal unitPrice = 0M;
                short quantity = 0;
                float discount = 0F;

                if (!System.Convert.IsDBNull(e.Row.Cells["UnitPrice"].Value))
                {
                    unitPrice = System.Convert.ToDecimal(e.Row.Cells["UnitPrice"].Value);
                }
                if (!System.Convert.IsDBNull(e.Row.Cells["Quantity"].Value))
                {
                    quantity = System.Convert.ToInt16(e.Row.Cells["Quantity"].Value);
                }
                if (!System.Convert.IsDBNull(e.Row.Cells["Discount"].Value))
                {
                    discount = System.Convert.ToSingle(e.Row.Cells["Discount"].Value);
                }
                e.Row.Cells["Amount"].Value = (float)unitPrice * quantity * (1 - discount);
            }
        }
    }
}
